package it.com.Test.a4_either;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestOfRunAfterEither {
    static ExecutorService executor = Executors.newFixedThreadPool(3);

    // 一号和二号任意一个执行完成，开始执行三号任务
    public static void main(String[] args) {
        System.out.println("main....start....");
        CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
            System.out.println("任务一开始");
            System.out.println("任务一结束***");
        }, executor);
        CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
            System.out.println("任务二开始");
            try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println("任务二结束***");
        }, executor);

        task1.runAfterEitherAsync(task2, () -> {
            System.out.println("任务三开始");
            System.out.println("任务三结束***");
        }, executor);
        System.out.println("main....end....");
    }
    /**
     main....start....
     任务一开始
     任务一结束***
     任务二开始
     main....end....
     任务三开始
     任务三结束***
     任务二结束***
     */
}
